import { ApiProperty, IntersectionType, PartialType } from '@nestjs/swagger';
import { Type } from 'class-transformer';
import {
  IsNumber,
  IsOptional,
  IsString,
  Matches,
  MaxLength,
  MinLength,
  IsArray,
  Min,
  IsEnum,
} from 'class-validator';
import { ProductStatus } from '~/shared/mongodb/schema/product.schema';

import { PagerDto } from '~/common/dto/pager.dto';

// 创建
export class CreateProductDto {
  @ApiProperty({ description: '商品名称', minLength: 2, maxLength: 100 })
  @IsString()
  @MinLength(2)
  @MaxLength(100)
  name: string;

  @ApiProperty({ description: '所属分类ID' })
  @IsString()
  categoryId: string;

  @ApiProperty({ description: '销售价格', minimum: 0 })
  @IsNumber()
  @Min(0)
  sellingPrice: number;

  @ApiProperty({ description: '原价', minimum: 0 })
  @IsOptional()
  originalPrice?: number;

  @ApiProperty({
    description: '商品状态',
    enum: ProductStatus,
    default: ProductStatus.ON_SALE,
  })
  @IsEnum(ProductStatus)
  status: ProductStatus = ProductStatus.ON_SALE;

  @ApiProperty({ description: '商品展示图URL' })
  @IsString()
  mainImage: string;

  @ApiProperty({ description: '商品详情图URL数组，最多5张' })
  @IsArray()
  detailImages: string[];

  @ApiProperty({ description: '商品简介' })
  @IsString()
  simpleDesc: string;

  @ApiProperty({ description: '商品详细描述' })
  @IsString()
  description: string;

  @ApiProperty({ description: '商品特色数组' })
  @IsArray()
  features: string[];

  @ApiProperty({ description: '配料信息', required: false })
  @IsOptional()
  @IsString()
  ingredients?: string;
}

// 更新
export class UpdateProductDto extends PartialType(CreateProductDto) {}

//查询
export class QueryProductDto extends PagerDto {
  @ApiProperty({ description: '商品名称（模糊查询）' })
  @IsOptional()
  @IsString()
  name?: string;

  @ApiProperty({ description: '所属分类ID' })
  @IsOptional()
  @Matches(/^[0-9a-fA-F]{24}$/, { message: '分类ID格式不正确' })
  categoryId?: string;

  @ApiProperty({ description: '商品状态', enum: ProductStatus })
  @IsOptional()
  @IsEnum(ProductStatus)
  status?: ProductStatus;

  @ApiProperty({ description: '排序' })
  @IsOptional()
  @IsString()
  sort?: string;
}
